package code.easy.contentstate.demo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import code.easy.contentstate.ContentState
import code.easy.contentstate.observerAsContentState
import code.easy.contentstate.rememberContentState
import code.easy.contentstate.ui.theme.EasyContentStateTheme

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            EasyContentStateTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting()
                }
            }
        }
    }
}

@Composable
fun Greeting() {
    val vm = viewModel<MainVM>()
    Column(modifier = Modifier.fillMaxSize()) {
        val lazyPagingItems = vm.pagingSource.flow.collectAsLazyPagingItems()
        var contentState = lazyPagingItems.observerAsContentState()
        MyContentState(contentState = contentState, modifier = Modifier.weight(1f)) {
            LazyColumn {
                items(lazyPagingItems){ index ->
                    Text(text = "item item item item item item item item item $index")
                }
            }
        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(50.dp)
        ) {
//            TextButton(onClick = { contentState = ContentState.Loading }) {
//                Text(text = "Loading")
//            }
//            TextButton(onClick = { contentState = ContentState.Empty }) {
//                Text(text = "Empty")
//            }
//            TextButton(onClick = { contentState = ContentState.Error }) {
//                Text(text = "Error")
//            }
//            TextButton(onClick = { contentState = ContentState.Content }) {
//                Text(text = "Content")
//            }
            TextButton(onClick = {
                vm.pagingSource.data.clear()
                vm.pagingSource.notifyDataSetChange()
            }) {
                Text(text = "Remove")
            }
            TextButton(onClick = {
                vm.pagingSource.data.add("ADD")
                vm.pagingSource.notifyDataSetChange()
            }) {
                Text(text = "Add")
            }
        }
    }

}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    EasyContentStateTheme {
    }
}